//
//  LMTitleButton.swift
//  WeiBo
//
//  Created by Li MAc on 2018/11/27.
//  Copyright © 2018 Li MAc. All rights reserved.
//

import UIKit

class LMTitleButton: UIButton {
    
    
     init(title:String) {
        super.init(frame: CGRect())
        
        
        if title == nil{
            setTitle("首页", for: [])
        }else{
            setTitle(title, for: [])
        }
       
        let but = UIButton.cz_textButton(title, fontSize: 17, normalColor: UIColor.red, highlightedColor: UIColor.black)
        
        but?.setImage(UIImage(named: "navigationbar_arrow_up"), for: [])
        but?.setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        
        //设置大小
        sizeToFit()
    }
    
    //重新布局子视图 (layoutSubviews)
    override func layoutSubviews() {
        super.layoutSubviews()
        //titleLabel?  imageView? 是可选  守护
        guard let titLabel = titleLabel , let imgView = imageView else{
            return
        }
        
        titLabel.frame = titLabel.frame.offsetBy(dx: -imgView.bounds.width, dy: 0)
        
        imgView.frame = imgView.frame.offsetBy(dx: titLabel.bounds.width, dy: 0)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
